package com.qys.livesMall.shop.service.impl;

import com.alibaba.fastjson2.JSON;
import com.qys.livesMall.app.user.model.entity.H2AppUser;
import com.qys.livesMall.common.exception.enums.ExceptionEnum;
import com.qys.livesMall.common.message.MessageServer;
import com.qys.livesMall.common.utils.bean.BeanProUtils;
import com.qys.livesMall.finance.enums.AccountBusinessTypeEnum;
import com.qys.livesMall.finance.enums.FinanceMessageEnum;
import com.qys.livesMall.finance.model.dto.AccountDetailSearch;
import com.qys.livesMall.finance.model.entity.shop.FinanceShopAccountDetail;
import com.qys.livesMall.finance.model.entity.shop.FinanceShopEarnestMoneyAccountDetail;
import com.qys.livesMall.finance.service.FinanceShopAccountDetailService;
import com.qys.livesMall.finance.service.FinanceShopEarnestMoneyAccountDetailService;
import com.qys.livesMall.framework.utils.SecurityUtils;
import com.qys.livesMall.framework.view.Paging;
import com.qys.livesMall.framework.view.QysPage;
import com.qys.livesMall.shop.dao.H2ShopBalanceDetailDao;
import com.qys.livesMall.shop.dao.H2ShopBaseDao;
import com.qys.livesMall.shop.enums.ShopMessageEnum;
import com.qys.livesMall.shop.mapper.H2ShopBalanceDetailMapper;
import com.qys.livesMall.shop.modal.dto.shop.balance.ShopBalanceDetailSearch;
import com.qys.livesMall.shop.modal.dto.shop.balance.ShopBalanceHandle;
import com.qys.livesMall.shop.modal.entity.*;
import com.qys.livesMall.shop.modal.vo.ShopBalanceDetailVO;
import com.qys.livesMall.shop.modal.vo.ShopBalanceVO;
import com.qys.livesMall.shop.service.H2ShopBalanceDetailService;
import com.qys.livesMall.shop.service.ShopOrderSkuService;
import com.qys.livesMall.system.source.domain.vo.dict.DictVO;
import com.qys.livesMall.system.source.service.ISysDictDataService;
import com.qys.livesMall.system.source.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 店铺余额明细 服务实现类
 * </p>
 *
 * @author
 * @since 2023-06-06
 */
@Service
public class H2ShopBalanceDetailServiceImpl  implements H2ShopBalanceDetailService {



    @Autowired
    private FinanceShopAccountDetailService financeShopAccountDetailService;

    @Autowired
    private FinanceShopEarnestMoneyAccountDetailService financeShopEarnestMoneyAccountDetailService;

    @Autowired
    private H2ShopBaseDao shopBaseDao;

    @Autowired
    private ISysDictTypeService dictDataService;

    @Autowired
    private MessageServer messageServer;

    @Autowired
    private ShopOrderSkuService shopOrderSkuService;

    @Override
    public QysPage<ShopBalanceDetailVO> list(AccountDetailSearch search) {
        QysPage<FinanceShopAccountDetail> page=financeShopAccountDetailService.list(search);
        if (CollectionUtils.isEmpty(page.getRecords())){
            return QysPage.build(search);
        }
        List<Long> shopIds = page.getRecords().stream().map(FinanceShopAccountDetail::getShopId).collect(Collectors.toList());
        List<H2ShopBase> shopBases = shopBaseDao.listByIds(shopIds);
        Map<Long, H2ShopBase> shopBaseMap = shopBases.stream().collect(Collectors.toMap(H2ShopBase::getId, v -> v));
        Locale locale= LocaleContextHolder.getLocale();
        List<DictVO> bus_typeVOS=dictDataService.selectDictDataByType("bus_type", SecurityUtils.getLanguage());
        Map<String,String> busMap=bus_typeVOS.stream().collect(Collectors.toMap(DictVO::getDictValue,DictVO::getDictLabel));
        List<DictVO> bus_type_detailVOS=dictDataService.selectDictDataByType("bus_type_detail",SecurityUtils.getLanguage());
        Map<String,String> busDetailMap=bus_type_detailVOS.stream().collect(Collectors.toMap(DictVO::getDictValue,DictVO::getDictLabel));
        List<ShopBalanceDetailVO> vos=new ArrayList<>();
        page.getRecords().forEach(e->{

            ShopBalanceDetailVO vo=BeanProUtils.copyProperties(e,ShopBalanceDetailVO::new);
            vo.setCounterparty(messageServer.getMsg(ExceptionEnum.PLATFORM));
            vo.setStatus("success");
            if (Objects.equals(vo.getCode(),AccountBusinessTypeEnum.SHOP_RECHARGE_CANCEL.getCode())||Objects.equals(vo.getCode(),AccountBusinessTypeEnum.SHOP_WITHDRAWAL_CANCEL.getCode())){
                vo.setStatus("reject");
            }
            vo.setProductDesc(messageServer.getMsg(FinanceMessageEnum.getEnum(vo.getCode())));
            if (Objects.equals(vo.getCategory(),AccountBusinessTypeEnum.ORDER_PROFIT.getType())){
                //查询订单和店铺
                Map<String,String> order= shopOrderSkuService.getByOrderNo(vo.getBusinessNo());
                if (order==null){
                    vo.setShopName(shopBaseMap.get(e.getShopId()).getShopName());
                    vo.setCategory(busMap.get(vo.getCategory()));
                    vo.setCode(busDetailMap.get(vo.getCode()));
                    vo.setIcon(AccountBusinessTypeEnum.getIcon(vo.getCode()).getUrl());
                    vos.add(vo);
                    return;
                }
                if (Objects.equals(vo.getCode(),AccountBusinessTypeEnum.ORDER_PROFIT.getCode())){
                    vo.setCounterparty(order.get("userName"));
                }
                vo.setProductDesc(order.get("name"));
            }
            vo.setShopName(shopBaseMap.get(e.getShopId()).getShopName());
            vo.setCategory(busMap.get(vo.getCategory()));
            vo.setCode(busDetailMap.get(vo.getCode()));
            vo.setIcon(AccountBusinessTypeEnum.getIcon(vo.getCode()).getUrl());
            vos.add(vo);
        });
        return QysPage.build(search).setRecords(vos).setTotal(page.getTotal());
    }

    @Override
    public QysPage<ShopBalanceDetailVO> emlist(AccountDetailSearch search) {
        QysPage<FinanceShopEarnestMoneyAccountDetail> page=financeShopEarnestMoneyAccountDetailService.list(search);
        if (CollectionUtils.isEmpty(page.getRecords())){
            return QysPage.build(search);
        }
        List<Long> shopIds = page.getRecords().stream().map(FinanceShopEarnestMoneyAccountDetail::getShopId).collect(Collectors.toList());
        List<H2ShopBase> shopBases = shopBaseDao.listByIds(shopIds);
        Map<Long, H2ShopBase> shopBaseMap = shopBases.stream().collect(Collectors.toMap(H2ShopBase::getId, v -> v));
        Locale locale= LocaleContextHolder.getLocale();
        List<DictVO> bus_typeVOS=dictDataService.selectDictDataByType("bus_type", SecurityUtils.getLanguage());
        Map<String,String> busMap=bus_typeVOS.stream().collect(Collectors.toMap(DictVO::getDictValue,DictVO::getDictLabel));
        List<DictVO> bus_type_detailVOS=dictDataService.selectDictDataByType("bus_type_detail",SecurityUtils.getLanguage());
        Map<String,String> busDetailMap=bus_type_detailVOS.stream().collect(Collectors.toMap(DictVO::getDictValue,DictVO::getDictLabel));
        List<ShopBalanceDetailVO> vos=new ArrayList<>();
        page.getRecords().forEach(e->{
            ShopBalanceDetailVO vo=BeanProUtils.copyProperties(e,ShopBalanceDetailVO::new);
            vo.setCounterparty(messageServer.getMsg(ExceptionEnum.PLATFORM));
            vo.setStatus("success");
            vo.setProductDesc(messageServer.getMsg(FinanceMessageEnum.getEnum(vo.getCode())));
            vo.setShopName(shopBaseMap.get(e.getShopId()).getShopName());
            vo.setCategory(busMap.get(vo.getCategory()));
            vo.setCode(busDetailMap.get(vo.getCode()));
            vo.setIcon(AccountBusinessTypeEnum.getIcon(vo.getCode()).getUrl());
            vos.add(vo);
        });
        return QysPage.build(search).setRecords(vos).setTotal(page.getTotal());
    }
}
